#include <clock.h>
#include <stdio.h>

volatile size_t ticks;

static inline uint64_t get_cycles(void) {
    uint64_t n;
    __asm__ __volatile__("rdtime %0" : "=r"(n));
    return n;
}

static uint64_t timebase = 100000;

void time_print(void) {
    uint64_t time = get_cycles();
    printf("time: %ld\n", time);
}

/* 开启时钟中断 */
void clock_init(void) {
    set_csr(sie, MIP_STIP);  // enable s timer interrapt
    clock_set_next_event();
    ticks = 0;
    printf("++ setup timer interrupts\n");
}

void clock_set_next_event(void) {
    sbi_set_timer(get_cycles() + timebase);
}